package com.drought.rained.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.drought.rained.R;
import com.drought.rained.bean.AuthInfoDataLJ;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ContactAdapterLJ extends RecyclerView.Adapter<ContactAdapterLJ.ContactViewHolder> {
    private Context context;
    private List<AuthInfoDataLJ.ContactsInfoBean.ContactsInfosBean> data;
    private OnSelectContactListener onSelectContactListener;
    private OnSelectRelationListener onSelectRelationListener;
    private boolean isOPPO;

    public void setModel(boolean model) {
        isOPPO = model;
        notifyDataSetChanged();
    }

    public ContactAdapterLJ(Context context, List<AuthInfoDataLJ.ContactsInfoBean.ContactsInfosBean> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public ContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_contact_layout, parent, false);
        return new ContactViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ContactViewHolder holder, int position) {
        holder.tvContact.setText(context.getString(R.string.contact_auth_contact) + (position + 1));
        holder.et_name.setText(data.get(position).getUserName());
        holder.et_name.setTag(data.get(position).getUserName());
        holder.et_phone.setText(data.get(position).getMobile());
        holder.et_relation.setText(data.get(position).getRelationValue());
        holder.et_relation.setTag(data.get(position).getRelation());

        if (isOPPO) {
            holder.et_phone.setHint(context.getResources().getString(R.string.contact_auth_phone_hint_enter));
            holder.et_phone.setFocusable(true);
            holder.et_phone.setFocusableInTouchMode(true);
            holder.rl_phone.setOnClickListener(null);
            holder.iv_phone.setVisibility(View.INVISIBLE);
        } else {
            holder.iv_phone.setVisibility(View.VISIBLE);
            holder.rl_phone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onSelectContactListener != null && !isOPPO) {
                        onSelectContactListener.onClick(holder.rl_phone, holder.getLayoutPosition());
                    }
                }
            });

            holder.et_phone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onSelectContactListener != null&& !isOPPO) {
                        onSelectContactListener.onClick(holder.et_phone, holder.getLayoutPosition());
                    }
                }
            });
        }

        holder.et_relation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSelectRelationListener != null) {
                    onSelectRelationListener.onClick(holder.et_relation, holder.getLayoutPosition());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class ContactViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_contact)
        TextView tvContact;
        @BindView(R.id.et_name)
        EditText et_name;
        @BindView(R.id.et_relation)
        EditText et_relation;
        @BindView(R.id.et_phone)
        EditText et_phone;
        @BindView(R.id.iv_phone)
        ImageView iv_phone;
        @BindView(R.id.rl_phone)
        RelativeLayout rl_phone;

        public ContactViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnSelectContactListener {
        void onClick(View view, int position);
    }

    public void setOnSelectContactListener(OnSelectContactListener onSelectContactListener) {
        this.onSelectContactListener = onSelectContactListener;
    }


    public interface OnSelectRelationListener {
        void onClick(View view, int position);

    }

    public void setOnSelectRelationListener(OnSelectRelationListener onSelectRelationListener) {
        this.onSelectRelationListener = onSelectRelationListener;
    }


}
